﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gibbed.IO;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using System.Collections.ObjectModel;

namespace AmaroK86.MassEffect3.Texture
{
    public class TFCEditor
    {
        public TFCEditor(string tfcFileName)
        {
            if (!File.Exists(tfcFileName))
            {
                new FileLoadException();
            }
            tfcFullPath = tfcFileName;
            tfcFileName = Path.GetFileName(tfcFullPath);
            tfcDirectory = Path.GetDirectoryName(tfcFullPath);
        }
        public string tfcFileName = "";
        public string tfcFullPath = "";
        public string tfcDirectory = "";
        //public TextureDatabase texDB = new TextureDatabase();
        public SortedList<long,string> offList = new SortedList<long,string>();

        //FileStream debugStream;

        internal class CompressedSegmentInfo
        {
            public uint UncompressedSize;
            public uint CompressedSize;

            public CompressedSegmentInfo(uint cSize, uint uSize)
            {
                CompressedSize = cSize;
                UncompressedSize = uSize;
            }
        }

        public void openTFC()
        {
            //debugStream = new FileStream("debug.txt", FileMode.Create, FileAccess.Write);

            //string XMLFileName = Path.GetFileNameWithoutExtension(fileName) + ".xml";
            /*if (File.Exists(XMLFileName))
            {
                ReadFromXML(XMLFileName);
                return;
            }*/

            FileStream texStream = new FileStream(tfcFullPath, FileMode.Open, FileAccess.Read);

            ulong checkVal = 0x000200009E2A83C1;
            int uncSize = 0;
            int compSize = 0;

            texStream.Seek(16, SeekOrigin.Begin);
            while (texStream.Position < texStream.Length)
            {
                if (texStream.ReadValueU64() == checkVal)
                {
                    long offset = texStream.Position - 8;
                    compSize = texStream.ReadValueS32();
                    uncSize = texStream.ReadValueS32();

                    int segmentCount = (int)Math.Ceiling((float)uncSize / (float)0x20000);
                    var segmentInfos = new CompressedSegmentInfo[segmentCount];
                    for (uint i = 0; i < segmentCount; i++)
                    {
                        segmentInfos[i] = new CompressedSegmentInfo(texStream.ReadValueU32(),texStream.ReadValueU32());
                    }

                    //TextureInfo texInfo = new TextureInfo();
                    /*texInfo.comprSize = compSize;
                    texInfo.uncSize = uncSize;*/
                    //tfcOffsetList.Add(offset, texInfo);
                    offList.Add(offset,"");

                    texStream.Seek(compSize, SeekOrigin.Current);
                }
                else
                    break;
            }

            //texDB.buildList();

            //debugStream.Close();
        }// end of main


    }
}
